package com.insurance.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.insurance.common.common.ResponseData;
import com.insurance.common.constant.ConstantFlag;
import com.insurance.common.dto.ProposalDto;
import com.insurance.common.enums.CommonEnum;
import com.insurance.common.utils.JwtOperatorUtil;
import com.insurance.entity.ProposalEntity;
import com.insurance.service.ProposalService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Style
 * @version 1.0
 * @date 2021/3/10
 **/
//@Api(tags = "投保单服务")
@Slf4j
//@RestController
//@RequestMapping("/proposal")
public class ProposalController {

    /**
     * 投保单service
     */
    @Autowired
    private ProposalService proposalService;

    /**
     * 分页查询投保单列表
     *
     * @param page 分页条件
     * @param userToken 用户凭证
     * @return
     */
    @ApiOperation(value = "分页查询投保单列表")
    @PostMapping("/queryProposalPage")
    public ResponseData<Page<ProposalDto>> queryProposalPage(Page page, @RequestHeader(value = "Authorization") String token, @RequestHeader String userToken){
        String userId = "";
        try {
            //校验用户凭证
            if(JwtOperatorUtil.isTokenExpired(userToken)){
                log.error("【查询投保单列表失败, 用户凭证错误】");
                return ResponseData.error(CommonEnum.USER_TOKEN_ERROR);
            }
            //获取用户主键
            userId = JwtOperatorUtil.getUserId(userToken);
            if(StringUtils.isBlank(userId)){
                log.error("【查询投保单列表失败, 用户为空】");
                return ResponseData.success(CommonEnum.USER_TOKEN_ERROR);
            }
            //分页查询投保单
            Page<ProposalDto> proposalPageInfo = proposalService.queryIProposalPage(page, userId, null, null);
            return ResponseData.success(proposalPageInfo);
        } catch (Exception e) {
            log.error("【用户：{}, 查询投保单列表失败, 系统异常：{}】", userId, e.getMessage());
            return ResponseData.error(CommonEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 通过投保单Id查询投保单详情
     *
     * @param proposalId 投保单主键
     * @return
     */
    @ApiOperation(value = "通过投保单Id查询投保单详情")
    @PostMapping("/getProposalDetail/{proposalId}")
    public ResponseData<ProposalDto> getProposalDetail(@PathVariable String proposalId, @RequestHeader(value = "Authorization") String token){
        try {
            //通过主键查询投保单
            ProposalEntity proposal = proposalService.getById(proposalId);
            if(proposal == null){
                log.error("【投保单Id：{}, 通过主键查询投保单失败, 投保单不存在】", proposalId);
                return ResponseData.error(CommonEnum.QUERY_ERROR_NODATA);
            }
            ProposalDto proposalDto = new ProposalDto();
            BeanUtils.copyProperties(proposal, proposalDto);
            return ResponseData.success(proposalDto);
        } catch (Exception e) {
            log.error("【投保单Id：{}, 通过主键查询投保单失败, 系统异常：{}】", proposalId, e.getMessage());
            return ResponseData.error(CommonEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 保存投保单
     *
     * @param proposalDto 投保单
     * @return
     */
    @ApiOperation(value = "保存投保单")
    @PostMapping("/saveProposal")
    public ResponseData saveProposal(ProposalDto proposalDto, @RequestHeader(value = "Authorization") String token, @RequestHeader String userToken){
        ProposalEntity proposalEntity = new ProposalEntity();
        BeanUtils.copyProperties(proposalDto, proposalEntity);
        String checkProposal = checkProposal(proposalEntity);
        if(!ConstantFlag.TRUE_STR.equals(checkProposal)){
            return ResponseData.error(checkProposal);
        }
        String appId = JwtOperatorUtil.getAppId(token);
        String userId = JwtOperatorUtil.getUserId(userToken);
        String userName = JwtOperatorUtil.getUserName(userToken);
        proposalEntity.setAppId(appId);
        proposalEntity.setCreateTime(new Date());
        proposalEntity.setCreatePersonId(userId);
        proposalEntity.setCreatePersonName(userName);
        boolean flag = proposalService.save(proposalEntity);
        return flag ? ResponseData.success(proposalEntity.getProposalId()) : ResponseData.error(CommonEnum.SAVE_ERROR);
    }

    /**
     * 修改投保单状态
     *
     * @param proposalId 投保单id
     * @param state 状态
     * @return
     */
    @PostMapping("/changeProposalState/{proposalId}")
    public ResponseData changeProposalState(@RequestHeader(value = "Authorization") String token, @RequestHeader String userToken,
                                            @PathVariable String proposalId, @RequestParam String state){
        ProposalEntity proposal = proposalService.getById(proposalId);
        if(proposal == null){
            log.error("【投保单Id：{}, 通过主键查询投保单失败, 投保单不存在】", proposalId);
            return ResponseData.error(CommonEnum.QUERY_ERROR_NODATA);
        }
        proposal.setState(state);
        proposal.setUpdateTime(new Date());
        boolean flag = proposalService.updateById(proposal);
        return flag ? ResponseData.success(CommonEnum.OPERATION_SUCCESS) : ResponseData.error(CommonEnum.OPERATION_ERROR);
    }

    public String checkProposal(ProposalEntity proposalEntity){
        if(proposalEntity.getProductId() == null){
            return "产品id不能为空";
        }
        if(proposalEntity.getCreateTime() == null){
            proposalEntity.setCreateTime(new Date());
        }
        return ConstantFlag.TRUE_STR;
    }


}
